home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
kermit.columbia.edu
/
kermit.columbia.edu.tar
/
kermit.columbia.edu
/
newsgroups
/
misc.19981211-19990422
/
000396_news@watsun.cc.columbia.edu _Mon Mar 22 11:16:00 1999.msg
< prev
next >
Wrap
Internet Message Format
|
1999-04-21
|
6KB
Return-Path: <news@watsun.cc.columbia.edu>
Received: from newsmaster.cc.columbia.edu (newsmaster.cc.columbia.edu [128.59.59.30])
by watsun.cc.columbia.edu (8.8.5/8.8.5) with ESMTP id LAA14226
for <kermit.misc@watsun.cc.columbia.edu>; Mon, 22 Mar 1999 11:16:00 -0500 (EST)
Received: (from news@localhost)
by newsmaster.cc.columbia.edu (8.8.5/8.8.5) id LAA02385
for kermit.misc@watsun.cc.columbia.edu; Mon, 22 Mar 1999 11:10:28 -0500 (EST)
X-Authentication-Warning: newsmaster.cc.columbia.edu: news set sender to <news> using -f
From: dn5006@my-dejanews.com
Subject: Re: Associative Array in Kermit 95
Date: Mon, 22 Mar 1999 16:02:44 GMT
Organization: Deja News - The Leader in Internet Discussion
Message-ID: <7d5pit$on3$1@nnrp1.dejanews.com>
To: kermit.misc@watsun.cc.columbia.edu
In article <36F5ECE5.5E8A07D1@value.net>,
Mark Sapiro <msapiro@value.net> wrote:
> Frank da Cruz wrote:
> >>
> In article <7cttsc$47f$1@nnrp1.dejanews.com>, <dn5006@my-dejanews.com> wrote:
>
> : Associative array is a very useful feature in scripting language, it enables
> : Perl, Tcl etc. the implementation of comlex data structures. The following
> : script demonstrates that associative array can also be crafted in Kermit
> : 95. The script counts the unique words in a regular english, french, german,
> : etc. text file.
> :
> : open read testfile.txt
> : if fail end 1 Can't open testfile.txt
> : assign \%n 0 ; init register
> : while true {
> : read \%l ; read each line
> : if fail break ; until the end of file
> : while > \flength(\%l) 0 {
> : assign \%w \fbreak(\%l,{ }) ; split on space
> : xif defined \m(\%w) { ; word already seen?
> : _assign \%w \feval(\m(\%w) + 1) ; incr count this word
> : } else {
> : _assign \%w 1 ; init count this word
> : increment \%n ; next register
> : _assign \%n \%w ; register this word
> : }
> : assign \%l \fltrim(\fright(\%l,-
> : \feval(\flength(\%l)-\flength(\%w)))) ; shift to next word
> : }
> : }
> : for \%k 1 \%n 1 {
> : assign \%w \m(\%k) ; get word from register
> : echo <\m(\%w)> \%w ; display occurences
> : }
> :
> : This approach avoids the use of array which has to be declared in advance.
> : The script does not take into account the non alphanumeric characters.
> :
> : Dat Nguyen
> : Airline Telecommunications and Information Services
> : 770 Sherbrooke West
> : Montreal, Quebec
> : Canada H3A 1G1
> : Email dat.nguyen&sita.int
> :
> Excellent! I've had associative arrays on my list for quite a while, but
> the list so long and time so short. I've reformatted your script to fit
> in 80 columns.
>
> We plan to add a script library to the Kermit website -- this one will
> certainly go into it. Other submissions are welcome too; send them in!
> (Be sure to document the Kermit program and version and other relevant
> info.)
>
> A quick glance shows this script doesn't use any new (post-C-Kermit-6.0)
> features, some of which would make it simpler and faster, for example
> the new \fword() and \fsplit() functions for extracting words from strings,
> with specified break masks (e.g. to make sure punctuation does not count
> as part of word (e.g. "thing" and "thing."). Also you can convert each
> word to lowercase with \flower() so "Thing", "thing", and "THING" count as
> the same word, etc.
>
> Readers should take special note of the "_assign" verb, which is subtly
> different from "assign" (see p.457 of the manual).
>
> - Frank
> >>
>
> There is a problem with the above approach to implementing associative
> arrays. Namely, if one of the 'words' used as an index happens to be
> equal to or an initial substring of a pre-defined macro (e.g. "cautious" or
> "robust") or one of the macros that gets defined implicitly when certain
> programming constructs are used (e.g. "break " or "continue"), the
> "xif defined" test gives the "wrong" result and the word doesn't get
> counted (although the macro 'word' does get (re-)defined, most likely
> with an error message from \feval()).
>
> This problem could probably be avoided by prepending some prefix to
> each word before using it as a macro name.
>
> --
> Mark Sapiro <msapiro@value.net> The highway is for gamblers,
> San Francisco Bay Area, California better use your sense - B. Dylan
>
That's right, and I was about to improve the script as follows (but it still
won't eliminate the collision 100%):
open read testfile.txt
if fail end 1 Can't not open testfile.txt
assign \%n 0
while true {
read \%l
if fail break
echo \%l
while > \flength(\%l) 0 {
assign \%w \fbreak(\%l,{ })
xif defined \m(word_count[\%w]) {
_assign word_count[\%w] \feval(\m(word_count[\%w])+1)
} else {
_assign word_count[\%w] 1
increment \%n
_assign word_reg[\%n] \%w
}
assign \%l \fltrim(\fright(\%l,-
\feval(\flength(\%l)-\flength(\%w))))
}
}
for \%k 1 \%n 1 {
assign \%w \m(word_reg[\%k])
echo <\m(word_count[\%w])> \%w
}
Dat Nguyen
Airline Telecommunications and Information Services
770 Sherbrooke West
Montreal, Quebec
Canada H3A 1G1
Email dat.nguyen&sita.int
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own